package org.serg.match3;

import java.util.List;

import org.serg.match3.CustomArrayAdapter;



import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {
	protected CustomApplication app;
	
	protected PlayerDAO datasource;
	protected CustomSQLiteHelper dbHelper;
	
	protected List<UserModel> players;
	
	protected Intent intent;
	public EditText name;
	
	public ListView playersListView;
	ArrayAdapter<UserModel> dataAdapter;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        dbHelper = CustomSQLiteHelper.getInstance(this);
        SQLiteDatabase database = dbHelper.open(); 
        datasource = new PlayerDAO(database);
        
        name = (EditText) findViewById(R.id.form_name);
        Button sendButton = (Button)findViewById(R.id.form_button);
        sendButton.setOnClickListener(createPlayerListener);

        // Get the application instance
        app = (CustomApplication)getApplication();
        
        //players = datasource.getAllPlayers();
        //app.setPlayer(null);
        //dataAdapter = new CustomArrayAdapter(this, players);
    	
        playersListView = (ListView) findViewById(R.id.playersListView);
        
        //playersListView.setAdapter( dataAdapter);
        
        playersListView.setOnItemClickListener(startGameListener);
    }

	protected void onResume()
	{
	   super.onResume();

	   players = datasource.getAllPlayers();
       app.setPlayer(null);
       dataAdapter = new CustomArrayAdapter(this, players);
   	
       playersListView.setAdapter( dataAdapter);
       
	}
	
	private OnClickListener createPlayerListener = new OnClickListener() {
		
		public void onClick(View v) {
			EditText text = (EditText) findViewById(R.id.form_name);
			System.out.println("new user  "+text.getText().toString());
			UserModel user = datasource.createPlayer(text.getText().toString());
			
			players.add(user);
			dataAdapter.notifyDataSetChanged();
			app.setPlayer(user);
	    }
	};
	
	private OnItemClickListener startGameListener = new OnItemClickListener() {
		
    	@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			/*
			 * Toast.makeText(getApplicationContext(), "Click ListItem " +
			 * values[position], Toast.LENGTH_LONG) .show();
			 */
    		
    		UserModel user = (UserModel) dataAdapter.getItem(position);
    		app.setPlayer(user);
    		
    		dataAdapter.notifyDataSetChanged();
            
            Intent intent = new Intent(MainActivity.this, GameActivity.class);
			startActivity(intent);
		}
    	
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


